/**
 *@description 用户画像》营销创意偏好》线上活动
 *@author yhw
 */
import {
  marketingThemeQuery,
  marketingModelQuery,
  marketingValueQuery,
  onlineAllQuery
} from '../services/MarketingOnline';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'MarketingOnline',

  state: {
    marketingThemeData: null,
    marketingThemeLoading: false,
    marketingModelData: null,
    marketingModelLoading: false,
    marketingValueData: null,
    marketingValueLoading: false,
    onlineAllData: null,
    onlineAllLoading: false,
  },

  subscriptions: {},

  effects: { * marketingThemeQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'marketingThemeLoading'
      });

      const {
        data
      } = yield call(marketingThemeQuery, parse(payload));

      if (data) {

        let max = 100,
          temp = {
            indicator: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7'];
        data.data && data.data.map(function(item, index) {
          temp.value.push(item.value);
          max = Math.max(item.value, max);
        });

        data.data && data.data.map(function(item, index) {
          temp.indicator.push({
            text: item.name + "：\r\n" + parseInt(item.value).formatMoney() + "",
            max: max
          });
        });

        yield put({
          type: 'marketingThemeQuerySuccess',
          payload: {
            marketingThemeData: temp,
          },
        });
      } else {
        error("获取活动主题偏好失败");
      }
    },
    //===================================
    * marketingModelQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'marketingModelLoading'
      });

      const {
        data
      } = yield call(marketingModelQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'marketingModelQuerySuccess',
          payload: {
            marketingModelData: temp,
          },
        });
      } else {
        error("获取活动形式偏好失败");
      }
    },
    //===================================
    * marketingValueQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'marketingValueLoading'
      });

      const {
        data
      } = yield call(marketingValueQuery, parse(payload));

      if (data) {
        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'marketingValueQuerySuccess',
          payload: {
            marketingValueData: temp,
          },
        });
      } else {
        error("获取活动利益偏好失败");
      }
    },
    //===================================
    * onlineAllQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'onlineAllLoading'
      });

      const {
        data
      } = yield call(onlineAllQuery, parse(payload));

      if (data) {
        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'onlineAllQuerySuccess',
          payload: {
            onlineAllData: temp,
          },
        });
      } else {
        error("获取线上活动整体互动意愿失败");
      }
    },

  },

  reducers: {
    marketingThemeLoading(state) {
      return {...state,
        marketingThemeLoading: true
      };
    },
    marketingThemeQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        marketingThemeLoading: false
      };
    },
    //===================================
    marketingModelLoading(state) {
      return {...state,
        marketingModelLoading: true
      };
    },
    marketingModelQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        marketingModelLoading: false
      };
    },
    //===================================
    marketingValueLoading(state) {
      return {...state,
        marketingValueLoading: true
      };
    },
    marketingValueQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        marketingValueLoading: false
      };
    },
    //===================================
    onlineAllLoading(state) {
      return {...state,
        onlineAllLoading: true
      };
    },
    onlineAllQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        onlineAllLoading: false
      };
    },

  },
};